package com.example.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.domain.AskForLeave;
import com.example.result.Result;
import com.example.service.AskForLeaveService;
import com.example.tool.GsonTool;

/**
 * @author 袁小弘
 * @describe 请假控制层
 * @date 2020-02-18
 */
@RestController
@RequestMapping("/api")
public class AskForLeaveController {

    @Autowired
    AskForLeaveService askForLeaveService;

    // ===============================请假管理==============================================

    /**
     * @param json
     * @return 添加成功则提示成功信息，反之则提示失败信息
     * @describe 添加请假信息
     */
    @RequestMapping(value = "/leaves", method = RequestMethod.POST)
    public Result addAskForLeave(@RequestBody String json) {
        // 将JSON数据转换为AskForLeave对象实体
        JSONObject parseObject = JSON.parseObject(json);
        AskForLeave askForLeave = GsonTool.getGsonInstance().fromJson(parseObject.getString("json"), AskForLeave.class);

        return askForLeaveService.addAskForLeave(askForLeave);

    }

    /**
     * @param json
     * @return 更新成功则提示成功信息，反之则提示失败信息
     * @describe 更新请假信息
     */
    @RequestMapping(value = "/leaves", method = RequestMethod.PUT)
    public Result updateAskForLeave(@RequestBody String json) {
        // 将JSON数据转换为AskForLeave对象实体
        JSONObject parseObject = JSON.parseObject(json);
        AskForLeave askForLeave = GsonTool.getGsonInstance().fromJson(parseObject.getString("json"), AskForLeave.class);
        return askForLeaveService.updateAskForLeave(askForLeave);

    }

    /**
     * @param askId
     * @return 删除成功则提示成功信息，反之则提示失败信息
     * @describe 根据请假编号删除请假信息
     */
    @RequestMapping(value = "/leaves/{askId}", method = RequestMethod.DELETE)
    public Result deleteAskForLeaveById(@PathVariable("askId") String askId) {
        return askForLeaveService.deleteAskForLeaveById(askId);

    }

    /**
     * @return 以JSON格式返回所有班级信息
     * @describe 查询所有请假信息
     */
    @RequestMapping(value = "/leaves", method = RequestMethod.GET)
    public Result queryAllAskForLeave() {
        return askForLeaveService.queryAllAskForLeave();

    }

    /**
     * @param askId
     * @return 以JSON格式返回该请假信息
     * @describe 根据编号查询请假信息
     */
    @RequestMapping(value = "/leaves/{askId}", method = RequestMethod.GET)
    public Result queryAskForLeaveById(@PathVariable("askId") String askId) {
        return askForLeaveService.queryAskForLeaveById(askId);

    }

    /**
     * @param json
     * @return 添加成功则返回一个成功提示，反之则提示失败信息
     * @describe 通过Excel批量添加请假信息
     */
    @RequestMapping(value = "/leaves/excels", method = RequestMethod.POST)
    public Result addAskForLeaveByExcel(@RequestBody String json) {

        JSONObject parseObject = JSON.parseObject(json);
        List<AskForLeave> list = JSON.parseArray(parseObject.getString("json"), AskForLeave.class);

        return askForLeaveService.addAskForLeaveByExcel(list);

    }

    /**
     * @param Ids
     * @return 删除成功则返回一个成功提示，反之则提示失败信息
     * @describe 根据请假编号批量删除请假信息
     */
    @RequestMapping(value = "/leaves/dels", method = RequestMethod.POST)
    public Result deleteAskForLeaveByIds(@RequestBody Map<String, String> Ids) {

        return askForLeaveService.deleteAskForLeaveByIds(Ids);
    }

    /**
     * @param json
     * @return 查询成功则返回一个成功提示，反之则提示失败信息
     * @describe 请假信息模糊查询
     */
    @RequestMapping(value = "/leaves/serachs", method = RequestMethod.POST)
    public Result serachByInfo(@RequestBody String json) {

        JSONObject parseObject = JSON.parseObject(json);
        String[] info = parseObject.getString("json").split(",");

        return askForLeaveService.serachByInfo(info);

    }

    /**
     * @return 查询成功则返回一个成功提示，反之则提示失败信息
     * @describe 查询所有学生信息
     */
    @RequestMapping(value = "/leaves/studentInfos", method = RequestMethod.GET)
    public Result queryAllStudentInfo() {
        return askForLeaveService.queryAllStudentInfo();
    }

    /**
     * @return 查询成功则返回一个成功提示，反之则提示失败信息
     * @describe 查询所有学生请假信息
     */
    @RequestMapping(value = "/leaves/studentAndAskForLeaveInfos", method = RequestMethod.GET)
    public Result queryAllStudentAndAskForLeaveInfo() {
        return askForLeaveService.queryAllStudentAndAskForLeaveInfo();
    }

    /**
     * @param json
     * @return 查询成功则返回一个成功提示，反之则提示失败信息
     * @describe 根据学生ID查询学生请假信息
     */
    @RequestMapping(value = "/leaves/askForLeaveByUserId", method = RequestMethod.POST)
    public Result selectAskForLeaveByUserId(@RequestBody String json) {
        System.out.println(json);
        JSONObject parseObject = JSON.parseObject(json);
        Integer userId = Integer.parseInt(parseObject.getString("json"));

        return askForLeaveService.selectAskForLeaveByUserId(userId);
    }
}
